﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NCalculator.Operations
{
    [Serializable()]
    public class PowerNode : ValueNode
    {
        #region Attributes
        private int _degree;
        #endregion

        public PowerNode()
        {
            _value = 0;
            _degree = 2;
        }

        public PowerNode(double value)
        {
            _value = value;
            _degree = 2;
        }

        /// <summary>
        /// Converting to string
        /// </summary>
        /// <returns>equation converted to string</returns>
        public override string ToString()
        {
            return string.Format("{0}^{1} ", _value, _degree);
        }

        /// <summary>
        /// Get/Set n-degree of square root
        /// </summary>
        public int Degree
        {
            get { return _degree; }
            set
            {
                if (value > 2)
                    _degree = value;
            }
        }

        /// <summary>
        /// Calculating square root
        /// </summary>
        /// <returns>value as double</returns>
        public override double Evaluation()
        {
            for (int i = 1; i < _degree; i++)
            {
                _value *= _value;
            }

            return _value;
        }
    }
}
